<?php
header("content-type:text/html;charset=utf-8");
include_once "../lib/databases.php";
include_once "../lib/function.php";

//接收查询账单的类型
$type = isset($_POST['type']) ? $_POST['type'] : "";
//如果没有接收到参数无不做任何操作
if($type != ""){
    //组织sql语句
    $account_sql = "select * from account where user_id= {$_SESSION['user_info']['id']}";
    //接收到的类型为all
    if($type != "all"){
        $account_sql .= " and type = '{$type}' order by create_times desc";
    }else{
        $account_sql .= " order by create_times desc";
    }

    //按条件查询用户账单
    $res = $db->getMoreData($account_sql);
    //在所有记录中增加类型名称“收入“或“支出”，并将时间转换为目标格式
    foreach($res as $key=>$val){
        //增加类型名称
        if($val['type'] == "income"){
            $res[$key]['type_name'] = "收入";
        }else if($val['type'] == "expense"){
            $res[$key]['type_name'] = "支出";
        }
        //将时间戳转为时间格式
        $res[$key]['create_times'] = date("Y-m-d H:i:s",$val['create_times']);
    }
    //返回信息
    if($res){
        echo $fun->success("查询账单成功",$res);
    }else{
        echo $fun->error("查询失败");
    }
}

